<template>
	<view class="buy_popup">
		<view class="close" @click="$emit('close')">
			<image :src="`${baseImageUrl}/common/close1.png`"></image>
		</view>
		<view class="title">
			<view v-if="type==1">质押官银</view>
			<view v-else>取出官银</view>
		</view>
		<view class="total">
			<text v-if="type==1">当前可质押官银:{{silver}}</text>
			<text v-else>当前可取出官银:{{pledge_value}}</text>
			<view class="btn" @click="all_in">全部</view>
		</view>
		<view class="content">
			<view class="icon" @click="reduce">
				<image :src="`${baseImageUrl}/common/js.png`"></image>
			</view>
			<input type="text" placeholder="请输入数量" v-model="number" @input="change_number">
			<view class="icon" @click="add">
				<image :src="`${baseImageUrl}/common/zj.png`"></image>
			</view>
		</view>
		<view class="btn_view">
			<view class="cancel" @click="$emit('close')">取消</view>
			<view class="confirm" @click="submit">确定</view>
		</view>
	</view>
</template>

<script>
	import {
		pledgeOperate
	} from '@/request/api.js';
	// import {
	// 	captchaAppId
	// } from "@/utils/config";
	export default {
		props: {
			type: {
				type: [Number, String],
				default: ""
			},
			silver: {
				type: [Number, String],
				default: ""
			},
			pledge_value: {
				type: [Number, String],
				default: ""
			}
		},
		data() {
			return {
				number: "",
				total: 0.00
			}
		},
		created() {
			this.total = this.type == 1 ? this.silver : this.pledge_value;
		},
		methods: {
			all_in() {
				this.number = this.type == 1 ? this.silver : this.pledge_value;
			},
			reduce() {
				if (this.number == 0) {
					this.$u.toast("不可减少了");
					return false;
				}
				this.number--;
			},
			add() {
				if (this.number >= this.total) {
					this.$u.toast("不可增加了");
					return false;
				}
				this.number++;
			},
			change_number(e) {
				e.target.value = e.target.value.replace(/^0|[^\d]|[.]/g, "");
				this.$nextTick(() => {
					this.number = e.target.value;
				});
			},
			async submit() {
				if (this.number <= 0) {
					this.$u.toast("请输入数量");
					return false;
				}
				// const captcha = new TencentCaptcha(captchaAppId, async (res) => {
				// 	if (res.ret !== 0) return false;
				// 	let {
				// 		randstr,
				// 		ticket
				// 	} = res;
				let operate = this.type == 1 ? 'in' : 'out';
				await pledgeOperate({
					number: this.number,
					operate
				});
				this.$u.toast("操作成功");
				setTimeout(() => {
					this.$emit('close');
				}, 1500);
				// }, {});
				// captcha.show();
			}
		}
	}
</script>

<style scoped lang="scss">
	.buy_popup {
		width: 630rpx;
		height: 480rpx;
		padding: 40rpx 30rpx;
		box-sizing: border-box;
		background: url(#{$baseImageUrl}/bg/630_630.png) no-repeat;
		background-size: 100% 100%;
		display: flex;
		flex-direction: column;
		justify-content: space-between;
		position: relative;

		.close {
			width: 122rpx;
			height: 122rpx;
			position: absolute;
			right: 0rpx;
			top: 0rpx;
			transform: translate(50rpx, -50rpx);
			z-index: 2;

			image {
				width: 100%;
				height: 100%;
			}
		}

		.title {
			display: flex;
			justify-content: center;
			align-items: center;

			view {
				width: 256rpx;
				height: 30rpx;
				line-height: 30rpx;
				text-align: center;
				color: #494338;
				font-size: 42rpx;
				font-weight: 400;
				font-family: LiSu;
				background: url(#{$baseImageUrl}/home/title.png) no-repeat;
				background-size: 100% 100%;
			}
		}

		.total {
			display: flex;
			justify-content: center;
			align-items: center;

			text {
				color: #494338;
				font-size: 28rpx;
				font-weight: 400;
				font-family: SimSun;
			}

			.btn {
				width: 92rpx;
				height: 44rpx;
				line-height: 44rpx;
				text-align: center;
				color: #756768;
				font-size: 28rpx;
				font-weight: 400;
				font-family: SimSun;
				margin-left: 16rpx;
				background: url(#{$baseImageUrl}/common/btn4.png) no-repeat;
				background-size: 100% 100%;
			}
		}

		.content {
			height: 80rpx;
			padding: 0 35rpx;
			box-sizing: border-box;
			display: flex;

			.icon {
				width: 80rpx;
				height: 80rpx;

				image {
					width: 100%;
					height: 100%;
				}
			}

			input {
				flex: 1;
				height: 80rpx;
				color: #494338;
				font-size: 28rpx;
				font-weight: 400;
				font-family: SimSun;
				border-radius: 10rpx;
				box-shadow: 2rpx -1rpx 10rpx 0px rgba(3, 0, 0, 0.2);
				padding-left: 40rpx;
				box-sizing: border-box;
				background: #C0B5B6;
			}
		}

		.price {
			text-align: center;
			color: #494338;
			font-size: 28rpx;
			font-weight: 400;
			font-family: SimSun;
		}

		.btn_view {
			width: 100%;
			display: flex;
			justify-content: center;

			view {
				width: 210rpx;
				height: 80rpx;
				line-height: 80rpx;
				text-align: center;
				color: #FFFFFF;
				font-size: 30rpx;
				font-weight: 400;
				margin: 0 16rpx;
			}

			.cancel {
				background: url(#{$baseImageUrl}/common/btn2.png) no-repeat;
				background-size: 100% 100%;
			}

			.confirm {
				background: url(#{$baseImageUrl}/common/btn.png) no-repeat;
				background-size: 100% 100%;
			}
		}
	}
</style>